package com.example.SpringBoot_demo.controller;

import com.example.SpringBoot_demo.bean.BlackUser;
import com.example.SpringBoot_demo.bean.User;
import com.example.SpringBoot_demo.service.BlackListService;
import com.example.SpringBoot_demo.util.MD5Util;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/blackList")
@Controller
public class BlackLIstController {
    private static final int INITIALPAGE = 0;
    private static final int ELEMENTNUMB = 10;

    @Autowired
    private BlackListService blackListService;

    @GetMapping("viewBlackList")

    public String viewBlackList(HttpServletRequest request,Model model1,Model model2) {
        System.out.println(MD5Util.encode("kzj123"));
        return "viewBlackList";
    }
    @GetMapping("viewBlackListJSON")

    @ResponseBody
    public List<BlackUser> viewBlackListJSON(String page){
        int realPage=0;
        List<BlackUser>blackUserList=null;
        if(!page.equals("")||page!=null){
            realPage=Integer.parseInt(page);
        }
        if(realPage>0){
            blackUserList=blackListService.viewBlackList((realPage-1)*ELEMENTNUMB,ELEMENTNUMB);
        }else {
            blackUserList=blackListService.viewBlackList(INITIALPAGE, ELEMENTNUMB);
        }
        return blackUserList;
    }
    @GetMapping("viewUserListJSON")

    @ResponseBody
    public List<User> viewUserListJSON(String page){
        int realPage=0;
        List<User>userList=null;
        if(!page.equals("")||page!=null){
            realPage=Integer.parseInt(page);
        }
        System.out.println(page);
        System.out.println(realPage);
        if(realPage>0){
            userList=blackListService.viewUserList((realPage-1)*ELEMENTNUMB,ELEMENTNUMB);
        }else {
            userList=blackListService.viewUserList(INITIALPAGE, ELEMENTNUMB);
        }
        for(User user:userList){
            System.out.println(user.getUserid());
        }
        if(userList.size()!=0){
            for(User user:userList){
                if(user.getUsersex().equals("1")){
                    user.setUsersex("男");
                }else {
                    user.setUsersex("女");
                }
            }
        }
        return userList;
    }

    @GetMapping("searchBlackUser")

    @ResponseBody
    public List<BlackUser> searchBlackUser(String userid, String username) {
        List<BlackUser>blackUserList=null;
        if (!userid.equals("")||!username.equals("")) {
            blackUserList=blackListService.searchBlackUser(userid,username);
        }else {
            blackUserList=blackListService.viewBlackList(INITIALPAGE, ELEMENTNUMB);
        }
        return blackUserList;
    }

    @GetMapping("deleteBlackUser")
    public String deleteBlackUser(String userid) {
        String realuserid=userid.replace(" ","");
        System.out.println("传进来的id:"+realuserid);
        blackListService.deleteBlackUser(userid.replace(" ",""));
        //blackListService.deleteBlackUser(userid);
        //request.setAttribute("blackList", blackListService.deleteBlackUser(userid, INITIALPAGE, ELEMENTNUMB));
        return "viewBlackList";
    }
    @GetMapping("countPage")

    @ResponseBody
    public int countPage(String listname){
        int realPage=0;
        String realListName ="libusers";
        if(listname.equals("black")){
            realListName="libblacklist";
        }
        int countNumb=blackListService.countPage(realListName);
        if(countNumb%ELEMENTNUMB!=0) {
            realPage=countNumb/ELEMENTNUMB+1;
        }else {
            realPage=countNumb/ELEMENTNUMB;
        }
        return realPage;
    }
    @GetMapping("searchUser")

    @ResponseBody
    public List<User> searchUser(String userid, String username) {
        List<User>userList=null;
        if (!userid.equals("")||!username.equals("")) {
            userList=blackListService.searchUser(userid,username);
        }else {
            userList=blackListService.viewUserList(INITIALPAGE, ELEMENTNUMB);
        }
        return userList;
    }
    @GetMapping("addBlackUser")
    public String addBlackUser(String userid,String username){
        System.out.println("userid:"+userid);
        System.out.println("username:" + username);
        blackListService.addBlackUser(userid.replace(" ",""),username.replace(" ",""));
        return "viewBlackList";
    }
}
